package com.foreknow.elm.service.impl;

import com.foreknow.elm.dao.DeliveryAddressDao;
import com.foreknow.elm.dao.impl.DeliveryAddressDaoImpl;
import com.foreknow.elm.po.DeliveryAddress;
import com.foreknow.elm.service.DeliveryAddressService;
import com.foreknow.elm.util.DBUtil;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DeliveryAddressServiceImpl implements DeliveryAddressService {
    DeliveryAddressDao deliveryAddressDao = new DeliveryAddressDaoImpl();
    private Connection conn;
    //1. 根据用户编号查询所属送货地址
    @Override
    public List<DeliveryAddress> squeryById(String userId) {
        List<DeliveryAddress> list = new ArrayList<>();
        try {
            conn = DBUtil.getConnection();
            list = deliveryAddressDao.queryById(userId);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
           DBUtil.close();
        }
        return list;
    }
//根据送货地址编号查询送货地址
    @Override
    public DeliveryAddress squeryDABbId(Integer daId) {
        DeliveryAddress deliveryAddress = new DeliveryAddress();
        try {
            conn = DBUtil.getConnection();
            deliveryAddress = deliveryAddressDao.queryDABbId(daId);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }
        return deliveryAddress;
    }
//向送货地址表中添加一条记录
    @Override
    public int sinsert(String contactName, Integer contactSex, String contactTel, String address, String userId) {
        int a = 0;
        try {
            conn = DBUtil.getConnection();
            DBUtil.beginTransaction();
            a = deliveryAddressDao.insert(contactName,contactSex,contactTel,address,userId);
            DBUtil.commitTransaction();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                DBUtil.rollbackTransaction();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
            try {
                DBUtil.rollbackTransaction();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        } finally {
            DBUtil.close();
        }
        return a;
    }
//根据送货地址编号更新送货地址信息
    @Override
    public int supdate(DeliveryAddress deliveryAddress) {
        int a = 0;
        try {
            conn = DBUtil.getConnection();
            DBUtil.beginTransaction();
            a = deliveryAddressDao.update(deliveryAddress);
            DBUtil.commitTransaction();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                DBUtil.rollbackTransaction();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
            try {
                DBUtil.rollbackTransaction();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        } finally {
           DBUtil.close();
        }
        return a;
    }
//根据送货地址编号删除一条记录
    @Override
    public int sdelete(Integer daId) {
        int a = 0;
        try {
            conn = DBUtil.getConnection();
            DBUtil.beginTransaction();
            a = deliveryAddressDao.delete(daId);
            DBUtil.commitTransaction();

        } catch (SQLException e) {
            e.printStackTrace();
            try {
                DBUtil.rollbackTransaction();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
            try {
                DBUtil.rollbackTransaction();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        } finally {
            DBUtil.close();
        }
        return a;
    }
}
